// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.metadata;

import java.util.ArrayList;
import java.util.List;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class MetadataTable implements Cloneable {

    private MetadataDatabase metadataDatabase;

    private String tableName;

    private List<MetadataColumn> columns;

    /**
     * Qiang.Zhang.Adolf@gmail.com MetadataTable constructor comment.
     */
    public MetadataTable() {
        columns = new ArrayList<MetadataColumn>();
    }

    /**
     * Getter for columns.
     * 
     * @return the columns
     */
    public List<MetadataColumn> getColumns() {
        return this.columns;
    }

    /**
     * Sets the columns.
     * 
     * @param columns the columns to set
     */
    public void setColumns(List<MetadataColumn> columns) {
        this.columns = columns;
    }

    /**
     * Getter for metadataDatabase.
     * 
     * @return the metadataDatabase
     */
    public MetadataDatabase getMetadataDatabase() {
        return this.metadataDatabase;
    }

    /**
     * Sets the metadataDatabase.
     * 
     * @param metadataDatabase the metadataDatabase to set
     */
    public void setMetadataDatabase(MetadataDatabase metadataDatabase) {
        this.metadataDatabase = metadataDatabase;
    }

    /**
     * Getter for tableName.
     * 
     * @return the tableName
     */
    public String getTableName() {
        return this.tableName;
    }

    /**
     * Sets the tableName.
     * 
     * @param tableName the tableName to set
     */
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#clone()
     */
    @Override
    public MetadataTable clone() {

        MetadataTable table = null;
        table = new MetadataTable();
        table.setTableName(getTableName());
        List<MetadataColumn> cs = new ArrayList<MetadataColumn>();
        for (MetadataColumn metadataColumn : getColumns()) {
            MetadataColumn clone = metadataColumn.clone();
            clone.setMetadataTable(table);
            cs.add(clone);
        }
        table.setColumns(cs);

        return table;
    }

    public boolean sameAs(MetadataTable table, boolean ignoreName) {
        if (this == table) {
            return true;
        }
        if (!ignoreName) {
            if (!table.getTableName().equals(this.getTableName())) {
                return false;
            }
        }
        if (table.getColumns().size() != getColumns().size()) {
            return false;
        }
        for (int i = 0; i < getColumns().size(); i++) {
            MetadataColumn col = getColumns().get(i);
            MetadataColumn c = table.getColumns().get(i);
            if (!col.sameAs(c)) {
                return false;
            }
        }
        return true;
    }
}
